const _reader={
    len:100,
    indexSpan: null,
    index: -1,
    title: '',
    content: '',
    audio: null,
    tts: 'https://ss0.baidu.com/6KAZsjip0QIZ8tyhnq/text2audio?cuid=dict&lan=ZH&ctp=1&pdt=30&tex=',
    init: function() {
        //移除广告
        $('.close_discor').remove();
        $('.photo-content-title-text-main').remove();
        $('.top-ad-container').remove();
        $('.banner').remove();

        var _ = this;
        var div = document.createElement("div");
        div.style='position:fixed;right:1%;bottom:10px;z-index:1000;'

        var audio = _.audio = document.createElement("audio");
        audio.id='audio'
        audio.addEventListener('ended',function(){_.read()},false)
        div.append(audio)

        var span = _reader.indexSpan = document.createElement("span");
        span.style='display:inline-block;font-size:14px;'
        span.innerHTML='-1'
        div.append(span)

        var btn_text = document.createElement("button");
        btn_text.innerHTML='<解析文本>'
        btn_text.addEventListener('click', function(){_.text()}, false);
        div.append(btn_text)

        var btn_read = document.createElement("button");
        btn_read.innerHTML='<朗读>'
        btn_read.addEventListener('click', function(){_.read()}, false);
        div.append(btn_read)

        var btn_send = document.createElement("button");
        btn_send.innerHTML='<发送>'
        btn_send.addEventListener('click', function(){_.send()}, false);
        div.append(btn_send)

        document.getElementsByTagName('body')[0].append(div)
    },
    text: function() {
        //标题
        this.title=$('[name="keywords"]').attr('content')
        //内容
        var arr = [];
        $.each($('.content p'), function(index, val) {
            arr.push(val.innerText)
        });
        this.content=arr.join('');
        this.content=this.content.replace(/\n/g,"")
        this.index=0;
        this.indexSpan.innerText='0 <'+this.content.length+'> ';
    },
    read: function() {
        if (this.index==-1) {
            alert('无可读内容，请解析文本!');
            _reader.indexSpan.innerText='-1'
            return;
        } else{};
        //alert(_content)
        var content='', start=this.index*this.len, len=this.len;
        if (start+this.len > this.content.length) {
            len=this.content.length-start;
            this.index =-1;
        } else{
            this.index++;
        };
        content=this.content.substr(start, len);
        console.log(content)
        _reader.indexSpan.innerText=start + ' + ' + len + '<' + this.content.length + '>'

        var src=this.tts + content;
        audio.src=src
        audio.play()
    },
    send: function() {
        var _ = this;
        $.ajax({
            url: 'http://127.0.0.1:8070/tool/crawler/story/send',
            type: 'POST',
            dataType: 'json',
            data: {content: _.content,title:_.title},
            success:function(result){
                alert(result.message)
            }
        });

    }
};
